package cn.kwq.project_manger.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.Validator;
import cn.kwq.project_manger.common.R;
import cn.kwq.project_manger.common.RES;
import cn.kwq.project_manger.pojo.Feedback;
import cn.kwq.project_manger.pojo.dto.FeedbackDto;
import cn.kwq.project_manger.pojo.dto.FindGameDto;
import cn.kwq.project_manger.repostitory.FeedbackRepository;
import cn.kwq.project_manger.repostitory.NoticeRepository;
import cn.kwq.project_manger.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: kwq
 * @Date: 2023/02/02/15:45
 * @Description:
 */
@Slf4j
@Api(tags ="公告和反馈")
@RestController
@RequestMapping("/feedbackAndNotice/")
public class FeedbackAndNoticeController {
    @Autowired
    FeedbackRepository feedbackRepository;

    @Autowired
    NoticeService noticeService;

    @ApiOperation("提交反馈")
    @SaIgnore//可以匿名访问该接口
    @PostMapping("putFeedback")
    public RES putFeedback(@RequestBody FeedbackDto dto) {
        Long userId = null;
        Feedback feedback=new Feedback();
        feedback.setIsGuest(0);
        //判断游客是否
        try{
           userId= StpUtil.getLoginIdAsLong();
        }catch (NotLoginException e){
            feedback.setIsGuest(1);
        }
        //判断联系方式类型
        if(Validator.isEmail(dto.getConnect())){
            feedback.setConnectType(1);
        } else if (Validator.isEmail(dto.getContent())) {
            feedback.setConnectType(2);
        }else {
            feedback.setConnectType(3);
        }
        feedback.setContent(dto.getContent());
        feedback.setConnect(dto.getConnect());
        feedback.setCreatedDate(new Date());
        feedbackRepository.save(feedback);

        return R.ok("反馈成功");
    }

    @ApiOperation("获取全部公告")
    @SaIgnore//可以匿名访问该接口
    @GetMapping("getNotice")
    public RES getNotice(){
        return R.ok(noticeService.getAllNotice());
    }




}
